package inspire13.duckhuntroid;

import inspire13.duckhuntroid.DuckHuntroidView;
import inspire13.duckhuntroid.DuckHuntroidView.DuckHuntroidThread;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class DuckHuntroid extends Activity implements SensorEventListener
{
	private static final int MENU_START = 1;
	
    /** A handle to the thread that's actually running the animation. */
    private DuckHuntroidThread mDuckHuntroidThread;

    /** A handle to the View in which the game is running. */
    private DuckHuntroidView mDuckHuntroidView;
    
    private SensorManager mSensorManager;
    private Sensor mSensor;

	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.duckhuntroid_layout);
        
        mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        
        // get handles to the LunarView from XML, and its LunarThread
        mDuckHuntroidView = (DuckHuntroidView) findViewById(R.id.duckhuntroid);
        mDuckHuntroidThread = mDuckHuntroidView.getThread();

        // give the LunarView a handle to the TextView used for messages
        mDuckHuntroidView.setTextView((TextView) findViewById(R.id.text));

        if (savedInstanceState == null)
        {
            // we were just launched: set up a new game
        	mDuckHuntroidThread.setState(GameState.STATE_RUNNING);
            Log.w(this.getClass().getName(), "SIS is null");
        }
        else
        {
            // we are being restored: resume a previous game
        	mDuckHuntroidThread.restoreState(savedInstanceState);
            Log.w(this.getClass().getName(), "SIS is nonnull");
        }
    }
	

	 /**
     * Invoked when the Activity loses user focus.
     */
	@Override
    protected void onPause()
    {
        super.onPause();
        mSensorManager.unregisterListener(this);
        mDuckHuntroidView.getThread().pause(); // pause game when Activity pauses
    }

    /**
     * Notification that something is about to happen, to give the Activity a
     * chance to save state.
     *
     * @param outState a Bundle into which this Activity should save its state
     */
    @Override
    protected void onSaveInstanceState(Bundle outState)
    {
        // just have the View's thread save its state into our Bundle
        super.onSaveInstanceState(outState);
        mDuckHuntroidThread.saveState(outState);
        Log.w(this.getClass().getName(), "SIS called");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	super.onCreateOptionsMenu(menu);
        menu.add(0, MENU_START, 0, R.string.menu_start);
        return true;
    }
    
    /**
     * Invoked when the user selects an item from the Menu.
     *
     * @param item the Menu entry which was selected
     * @return true if the Menu item was legit (and we consumed it), false
     *         otherwise
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case MENU_START:
            	mDuckHuntroidThread.doStart();
                return true;
        }

        return false;
    }
    
    @Override
    protected void onResume()
    {
      super.onResume();
      mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }
    
    public void onSensorChanged(SensorEvent event)
    {
        float axisX = event.values[0];
        float axisY = event.values[1];
        
        mDuckHuntroidThread.UpdateCrosshair(axisY, axisX);
     }


	public void onAccuracyChanged(Sensor arg0, int arg1)
	{
		System.out.println(arg0);
	}
 }
